package components
{
	import mx.charts.AxisRenderer;
	import mx.charts.CategoryAxis;
	import mx.charts.ColumnChart;
	import mx.charts.GridLines;
	import mx.charts.HitData;
	import mx.charts.LinearAxis;
	import mx.charts.chartClasses.Series;
	import mx.charts.events.ChartItemEvent;
	import mx.charts.series.*;
	import mx.collections.ArrayCollection;
	import mx.graphics.SolidColor;
	import mx.graphics.Stroke;
	
	import org.quantitativesustainability.styles.Styles;

	public class PersonalTransportChart extends ColumnChart implements QSChartInterface
	{
		internal var styles:Styles=new Styles();

		private var _genInfo:String;
		private var _discussion:String;

		public function set genInfo(s:String):void
		{
			_genInfo=s;
		}

		public function get genInfo():String
		{
			return _genInfo;
		}

		public function set discussion(s:String):void
		{
			_discussion=s;
		}

		public function get discussion():String
		{
			return _discussion;
		}

		// AXES AND SERIES:
		// ... horizontal axis:

		public var hax:CategoryAxis=new CategoryAxis();
		public var vax:LinearAxis=new LinearAxis();
		//public var vaxRn:AxisRenderer=new AxisRenderer();

		// ... vertical series, their axes, and renderers:
		public var sIncCost:ColumnSeries=new ColumnSeries();

		public var sEF:PlotSeries=new PlotSeries();
		public var vaxEF:LinearAxis=new LinearAxis();
		public var vaxRnEF:AxisRenderer=new AxisRenderer();

		public var sGen:PlotSeries=new PlotSeries();
		public var vaxGen:LinearAxis=new LinearAxis();
		public var vaxRnGen:AxisRenderer=new AxisRenderer();

		public function PersonalTransportChart()
		{
			super();
		}

		public function init(dpp:ArrayCollection):void
		{
			this.dataProvider=dpp;

			// DATA-TIPS:
			this.dataTipFunction=_dataTipFunction;
			this.showDataTips=true;
			this.dataTipMode='single';
			//this.itemClick="displayInfo(event)";

			// HORIZONTAL AXIS:
			hax.dataProvider=this.dataProvider;
			hax.categoryField='label';
			hax.title='';
			this.horizontalAxis=hax;

			render();
			addSeries(); // Also set visibility of series and axes;
		}

		public function addSeries():void
		{
			// ... totalCost:
			//vax.title="Incremental cost [ c$ / km ]";
			//vax.minimum=0;

			vax.title="Incremental Cost [$ cent / km]";

			sIncCost.dataProvider=this.dataProvider;
			sIncCost.xField='label';
			sIncCost.yField='incrementalCost';
			sIncCost.setStyle('fill', new SolidColor(styles.c_sb2_3, 1));
			sIncCost.setStyle('stroke', new Stroke(styles.c_sb2_3));
			sIncCost.horizontalAxis=hax;
			sIncCost.verticalAxis=vax;

			// ... emissionFactor
			sEF.dataProvider=this.dataProvider;
			sEF.xField='label';
			sEF.yField='emissionFactorCO2';
			sEF.setStyle('stroke', null);
			sEF.setStyle('fill', new SolidColor(styles.c_sg0_0, 1));
			vaxEF.title="Emission factor  [ kg CO2 / km ]";
			vaxEF.minimum=0;
			sEF.verticalAxis=vaxEF;
			sEF.visible=false;
			vaxRnEF.visible=false;

			// ... add to graph:
			var series:Array=new Array();
			series.push(sIncCost);
			series.push(sEF);
			series.push(sGen);
			this.series=series;
		}

		public function render():void
		{
			// Get rid of shadows:
			this.seriesFilters=[];

			this.type='overlaid';

			var gl:GridLines=new GridLines();
			gl.y=0;
			gl.setStyle('direction', 'horizontal')
			gl.setStyle('verticalShowOrigin', false);
			gl.setStyle('horizontalStroke', styles.st_gridlines);
			gl.setStyle('horizontalOriginStroke', styles.st_origin);
			var bge:Array=new Array();
			bge.push(gl);
			this.backgroundElements=bge;

			var haxr:AxisRenderer=new AxisRenderer();
			haxr.axis=hax;
			haxr.placement='bottom';
			haxr.setStyle('axisStroke', new Stroke(0x000000, 1));
			haxr.setStyle('tickPlacement', 'none');
			haxr.setStyle('labelRotation', null);
			this.horizontalAxisRenderer=haxr;

			var vaxrs:Array=new Array();
			var vaxr:AxisRenderer=new AxisRenderer();
			vaxr.axis=vax;
			vaxr.placement='left';
			vaxr.setStyle('axisStroke', new Stroke(0x000000, 1));
			vaxr.setStyle('tickPlacement', 'none');
			vaxr.setStyle('labelRotation', null);
			vaxr.setStyle('verticalAxisTitleAlignment', 'vertical');

			vaxRnEF.axis=vaxEF;
			vaxRnEF.placement='right';
			vaxRnEF.setStyle('axisStroke', new Stroke(styles.c_sg0_2, 1));
			vaxRnEF.setStyle('tickPlacement', 'none');
			vaxRnEF.setStyle('verticalAxisTitleAlignment', 'vertical');
			vaxRnEF.setStyle('labelGap', 8);
			vaxRnEF.setStyle('color', styles.c_sg0_2);
			vaxrs.push(vaxRnEF);

			this.verticalAxisRenderer=vaxr;
			this.verticalAxisRenderers=vaxrs;
		}

		public function updateCarbonPrice(value:Number):void
		{
			for (var i:Object in dataProvider)
			{
				dataProvider[i].incrementalCost=
					// [kg / km] * [$ / ton] * {100 [c$ / $] * 1e-3 [ton / kg]}  
					dataProvider[i].emissionFactorCO2 * value * 0.1;
			}
		}

		private function _dataTipFunction(hd:HitData):String
		{
			var rtVal:String='<b>' + hd.item.label + '</b>\n--------------------------------\n';
			rtVal+='Emission factor (CO2) = '
				+ hd.item.emissionFactorCO2.toPrecision(2).toString()
				+ '&nbsp;[&nbsp;kg&nbsp;CO2&nbsp;/&nbsp;km&nbsp;]\n';
			rtVal+='<b>Incremental cost</b> = '
				+ hd.item.incrementalCost.toPrecision(2).toString()
				+ '&nbsp;[&nbsp;$&nbsp;cents&nbsp;/&nbsp;km&nbsp;]';
			return rtVal;
		}

		public function getChartItemInfo(e:ChartItemEvent):String
		{
			var rtVal:String='<i>No further information.</i>';
			if (e.hitData.item.info is String)
				rtVal=(e.hitData.item.info);
			return rtVal;
		}

		public function showSeries(a:AxisRenderer, s:Series):void
		{
			if (a.visible == true || s.visible == true)
			{
				a.visible=false;
				s.visible=false;
			}
			else
			{
				a.visible=true;
				s.visible=true;
			}
		}
	} //Class.
}